﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace Web.Context
{
    public class CurrentCache
    {
        #region Statistics

        public void StatisticsAdd(Dictionary<string, string> value)
        {
            this.AddToCache("RentalGlobalStats", value, 15);
        }

        public Dictionary<string, string> StatisticsGet()
        {
            return this.GetFromCache("RentalGlobalStats") as Dictionary<string, string>;
        }

        public void StatisticsClear()
        {
            this.RemoveFromCache("RentalGlobalStats");
        }

        #endregion

        #region private

        private void AddToCache(string key, object value, int expirationMinues)
        {
            HttpContext.Current.Cache.Insert(key, value, null, DateTime.Now.AddMinutes(expirationMinues), TimeSpan.Zero);
        }

        private object GetFromCache(string key)
        {
            if (String.IsNullOrEmpty(key))
                throw new ArgumentNullException("Cache: GetFromCache - key string");

            return HttpContext.Current.Cache.Get(key);
        }

        private void RemoveFromCache(string key)
        {
            if (String.IsNullOrEmpty(key))
                throw new ArgumentNullException("Cache: RemoveFromCache - key string");

            HttpContext.Current.Cache.Remove(key);
        }

        #endregion
    }
}